#define _CRT_SECURE_NO_WARNINGS

#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->capacity = 0;
	ps->top = 0;
	ps->date = NULL;;
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->date);
	ps->date = NULL;
	ps->capacity = ps->top = 0;
}

void checkCapacity(Stack* ps)
{
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDateType* temp = (STDateType*)realloc(ps->date, sizeof(STDateType) * newCapacity);
		assert(temp);
		ps->date = temp;
		ps->capacity = newCapacity;
	}
}

void StackPush(Stack* ps, STDateType x)
{
	assert(ps);
	checkCapacity(ps);
	ps->date[ps->top] = x;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top>0);
	ps->top--;
}

STDateType StackTop(Stack* ps)
{
	assert(ps);
	//assert(ps->top>0 );
	assert(!StackEmpty(ps));
	return (ps->date[ps->top - 1]);
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);
	//if (ps->date == NULL)
	//{
	//	return true;
	//}
	//else
	//{
	//	return false;
	//}

	return ps->top == 0;
}